/**
 * 广寒宫
 * 网址:www.guanghangong.xyz
 */
package com.moon.admin.login.controller;

import com.google.common.collect.Maps;
import com.moon.admin.sys.domain.vo.RouterVO;
import com.moon.admin.sys.entity.SysMenuEntity;
import com.moon.admin.sys.entity.SysModuleEntity;
import com.moon.admin.sys.service.SysModuleService;
import com.moon.admin.sys.service.SysRouterService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.moon.admin.framework.config.SystemConfig;
import com.moon.admin.framework.secure.AuthUtils;
import com.moon.admin.framework.secure.domain.AuthInfo;
import com.moon.admin.framework.springmvc.domain.R;
import com.moon.admin.framework.syslog.annotation.SysLog;
import com.moon.admin.login.service.LoginService;
import com.moon.admin.sys.service.SysMenuService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * 用户登录注册
 * @author ninglong
 */
@RestController
@Api(value = "用户登录", tags = "用户登录")
public class LoginController {

	@Autowired
	private LoginService loginService;
	@Autowired
	private SysMenuService menuService;
	@Autowired
	private SystemConfig systemConfig;
	@Autowired
	private SysRouterService sysRouterService;
	@Autowired
	private SysModuleService sysModuleService;
	
	@GetMapping("accountLogin")
	@ApiOperation(value = "账号登录", notes = "账号:account,密码:password")
	@SysLog(mod = "登录/登出",fun = "账号登录")
	public R<String> accountLogin(@ApiParam(value = "账号", required = true) @RequestParam String account,
							 @ApiParam(value = "密码", required = true) @RequestParam String password) {
		if(StringUtils.isEmpty(account)){
			return R.fail("账号不能为空");
		}
		if(StringUtils.isEmpty(password)){
			return R.fail("密码不能为空");
		}
		String token = loginService.login(account, password);
		return R.success(token);
	}
	
	@GetMapping("logout")
	@ApiOperation(value = "退出")
	@SysLog(mod = "登录/登出",fun = "用户退出")
	public R<Void> logout(){
		AuthUtils.clearAuthInfo(AuthUtils.getCurrentUserId());
		return R.success();
	}
	
	@GetMapping(value = "getInfo")
	@ApiOperation(value="获取当前用户信息,包含权限信息")
	public R<Map<String,Object>> getInfo() {
		AuthInfo authInfo = AuthUtils.getAuthInfo();
		Set<String> permissions = menuService.getMenuPermsByUserId(authInfo.getId());
		AuthUtils.setPermission(authInfo.getId(),permissions);
		Map<String,Object> info = Maps.newHashMap();
		info.put("user", authInfo);
		info.put("roles", authInfo.getRoleCodes());
		info.put("permissions", permissions);
		return R.success(info);
	}

	@GetMapping("getRouters")
	@ApiOperation(value="获取当前用户的路由信息")
	public R<Map<String,Object>> getRouters(){
		Integer userId = AuthUtils.getCurrentUserId();
		if(userId==null){
			return R.fail("当前用户未登录");
		}
		List<SysMenuEntity> menus = menuService.selectMenuTreeByUserId(userId);
		List<RouterVO> routerVos = sysRouterService.buildMenus(menus);
		List<SysModuleEntity> sysModuleEntityList = sysModuleService.list();
		Map<String,Object> resultMap = Maps.newHashMap();
		resultMap.put("routers",routerVos);
		resultMap.put("modules",sysModuleEntityList);
		return R.success(resultMap);
	}
	
	@GetMapping("offline")
	@ApiOperation(value = "强制用户下线")
	@SysLog(mod = "登录/登出",fun = "强制用户下线")
	public R<Void> offline(Integer userId){
		AuthUtils.clearAuthInfo(userId);
		return R.success();
	}
}